package com.wing.user.service;

import cn.hutool.core.util.StrUtil;
import com.wing.user.model.response.MenuVO;
import com.wing.user.model.response.WarehouseMenuVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.user.dao.WarehouseMenuDao;
import com.wing.user.model.entity.WarehouseMenu;
import com.wing.user.service.WarehouseMenuService;
@Service
public class WarehouseMenuServiceImpl extends ServiceImpl<WarehouseMenuDao, WarehouseMenu> implements WarehouseMenuService{



    @Override
    public List<Map<String, Object>> getMenuListByUserId(Long loginUserId) {
        return getMenuTree(baseMapper.getMenuListByUserId(loginUserId), -1L);
    }

    private List<Map<String, Object>> getMenuTree(List<WarehouseMenuVO> menus, Long parentId) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < menus.size(); i++) {
            WarehouseMenuVO temp = menus.get(i);
            if (parentId != null && temp.getParentId() != null && parentId.intValue() == temp.getParentId().intValue()) {
                Map<String, Object> map = new HashMap<>();
                map.put("name", temp.getName());
                map.put("icon", temp.getIcon());
                map.put("button", temp.getButton());
                map.put("url", StrUtil.isBlank(temp.getUrl()) ? "javascript:;" : temp.getUrl());
                List<Map<String, Object>> subMenus = getMenuTree(menus, menus.get(i).getId());
                map.put("subMenus", subMenus);
                //去除空菜单
                if(StringUtils.isBlank(temp.getUrl()) && subMenus.size() <= 0){
                    continue;
                }
                list.add(map);
            }
        }
        return list;
    }
}
